package com.shanjupay.merchant.service.impl;


import com.shanjupay.common.domain.BusinessException;
import com.shanjupay.common.domain.CommonErrorCode;
import com.shanjupay.common.util.QiniuUtils;
import com.shanjupay.merchant.service.FileService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class FileServiceImpl implements FileService {
    /*
    *
    * 上传文件实现
    * */
    //1从nacos配置文件中共进行获取上传文件需要的url 账号 密钥 buket
    @Value("${oss.qiniu.url}")
    private String qiniuUrl;
    @Value("${oss.qiniu.accessKey}")
    private String accessKey;
    @Value("${oss.qiniu.secretKey}")
    private String secretKey;
    @Value("${oss.qiniu.bucket}")
    private String bucket;

    @Override
    public String upload(byte[] bytes, String filename) throws BusinessException {
        //上传文件可能抛出异常 使用 try-catch包裹文件上传
        try{
            QiniuUtils.upload2Qiniu(accessKey,secretKey,bucket,bytes,filename);
        }catch (Exception e){
            e.printStackTrace();
            //抛出自定义文件上传错误异常
            throw new BusinessException(CommonErrorCode.E_100106);
        }

        return  qiniuUrl+filename;
    }
}
